HackerRank Construct the Array
https://www.hackerrank.com/challenges/construct-the-array/problem
解答
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'countArray' function below.
#
# The function is expected to return a LONG_INTEGER.
# The function accepts following parameters:
# 1. INTEGER n
# 2. INTEGER k
# 3. INTEGER x
#
def countArray(n, k, x):
# Return the number of ways to fill in the array.
MOD = 1000000007
a = 0 * n
b = 0 * n
a0 = 1 if x == 1 else 0
b0 = 0 if x == 1 else 1
for i in range(1, n):
ai = bi-1 % MOD
bi = (ai-1 * (k-1) + bi-1 * (k-2)) % MOD
return an-1
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input0)
k = int(first_multiple_input1)
x = int(first_multiple_input2)
answer = countArray(n, k, x)
fptr.write(str(answer) + '\n')
fptr.close()
メモ
https://www.youtube.com/watch?v=_nLeiMMSd4E
bi-1
https://scrapbox.io/files/61d24246effe30001d3a4450.png